%%% erl -sname "sim" -setcookie "max" -run control basic -noshell
-module(connector).
-export([init/0, start/0]).
-include("cell_records.hrl").

init() ->
	register(connector, spawn(connector, start, [])).


start() ->
  loop(#connector{}).

loop(#connector{state = State} = Conn) ->
	receive
	  %-------- data: simulation -> GUI ------------------%
    {veg_state, {X, Y}, Val} ->
      NewConn = handle({veg_state, X, Y, Val}, Conn),
	    loop(NewConn);
	  {dead, Id} ->
	    call(Conn#connector.host, {2, Id}),
      %io:format("~p~n", [{dead, Id}]),
	    loop(Conn);
    {new_thing, 0, Id, X, Y, SpawnW, MinW, W, Sp} ->
      call(Conn#connector.host, {3, 0, Id, X, Y, SpawnW, MinW, W, Sp}),
      io:format("~p~n", [{new_thing, 0, Id, X, Y, SpawnW, MinW, W, Sp}]),
	    loop(Conn);
    {fat_boy, Id, Veg} ->
      call(Conn#connector.host, {4, Id, Veg}),
      %io:format("~p~n", [{fat_boy, Id, Veg}]),
	    loop(Conn);
    {moved, Id, X, Y} ->
      call(Conn#connector.host, {5, Id, X, Y}),
      %io:format("~p~n", [{moved, Id, X, Y}]),
	    loop(Conn);
    %-------- data: GUI -> simulation ------------------%
    {size, X, Y} ->
      props ! {set, size, {X, Y}},
      io:format("~p~n", [floor(X * Y * 2 / 5)]),
	    loop(Conn#connector{max_msg = X});%floor(X * Y *2 / 10)});
	  {the_thing, X, Y, SpawnW, MinW, W, Speed} ->
      %io:format("~p~n", [{the_thing, X, Y, SpawnW, MinW, W, Speed}]),
	    props ! {the_thing, X, Y, SpawnW, MinW, W, Speed},
	    loop(Conn);
    {veg, Val} ->
      props ! {set, veg, Val},
      loop(Conn);
    {host, Host} ->
      %io:format("~p~n", [Conn#connector{host = Host}]),
      loop(Conn#connector{host = Host});
    %-------- control: GUI -> simulation ---------------%
    print_all ->
      io:format("~p~n", [Conn]),
      loop(Conn);
    init when State == stop -> % signal to create cells and everything, simulation then pauses and waits for play signal
      control ! init,
      %io:format("~p~n", [Conn#connector{state = pause}]),
	    loop(Conn#connector{state = pause});
    play when State == pause ->
      control ! play,
      %io:format("~p~n", [Conn#connector{state = play}]),
	    loop(Conn#connector{state = play});
    pause when State == play ->
      control ! pause,
      %io:format("~p~n", [Conn#connector{state = pause}]),
	    loop(Conn#connector{state = pause});
    stop when State /= stop ->
      control ! stop,
      %io:format("~p~n", [Conn#connector{state = stop}]),
	    loop(Conn#connector{state = stop});
    exit ->
      %io:format("~p~n", [Conn]),
      control ! exit;
      % no loop()
    Msg -> % ignore wrong messages
      io:format("Connector: Wrong message: '~p'~n", [Msg]),
      loop(Conn)
	end.

handle({veg_state, X, Y, Val}, #connector{msgs = List, msg_count = Count, max_msg = Max, host = Host} = Conn) when Count + 1 == Max ->
  call(Host, {1, [{X, Y, Val} | List]}),
  Conn#connector{msgs = [], msg_count = 0};
handle({veg_state, X, Y, Val}, #connector{msgs = List, msg_count = Count} = Conn) ->
  Conn#connector{msg_count = Count + 1, msgs = [{X, Y, Val} | List]}.


call(Host, X) ->
  %io:format("call: ~p ! ~p~n", [Host, X]),
	%{dummy, 'dummy@hopsiup'} ! X.
	%{any, 'c1@localhost'} ! X.
	{any, Host} ! X.
	%{any, 'c1@hopsiup'} ! X.

floor(X) ->
    T = erlang:trunc(X),
    case (X - T) of
        Neg when Neg < 0 -> T - 1;
        Pos when Pos > 0 -> T;
        _ -> T
    end.
